/**
 * @Description: 控制点管理
 * @Date 2017/12/23
 * @Authoer: weimei
 * @Modified By:
 */
define([
  './themes/lang/' + window.YUFP_SYS_CONFIG.language + '/pages/content/systemManager/resContrManage/resContrManage.js',
  './custom/widgets/js/yufpExtTree.js'
], function (require, exports) {
  exports.ready = function (hashCode, data, cite) {
    yufp.lookup.reg('HTTP_METHOD_TYPE');
    yufp.custom.vue({
      el: '#resContrManage',
      data: function () {
        var me = this;
        return {
          pointFormdata: {},
          height: yufp.custom.viewSize().height - 20,
          viewType: 'DETAIL',
          isChildNode: false,
          funcId: '',
          viewTitle: yufp.lookup.find('CRUD_TYPE', false),
          formDisabled: false,
          dialogVisible: false,
          createCheck: !yufp.session.checkCtrl('add'), // 新增按钮控制
          modifyCheck: !yufp.session.checkCtrl('modify'), // 修改按钮控制
          deleteCheck: !yufp.session.checkCtrl('delete'), // 删除按钮控制
          treeUrl: backend.appOcaService + '/api/adminsmrescontr/treequery',
          contrDataUrl: backend.appOcaService + '/api/adminsmrescontr/getcontrinfo',
          pointTableColumns: [
            {
              label: this.$t('resContrManage.kzczmc'),
              prop: 'contrName',
              width: 120,
              sortable: true,
              resizable: true,
              template: function () {
                return '<template scope="scope">\
                                 <a onclick="return false;" href="javascipt:void(0);" style="text-decoration:underline;" @click="_$event(\'custom-row-click\', scope)">{{ scope.row.contrName }}</a>\
                             </template>';
              }
            },
            { label: this.$t('resContrManage.kzczdm'), prop: 'contrCode', width: 150, sortable: true, resizable: true },
            { label: this.$t('resContrManage.kzcz'), prop: 'contrUrl', sortable: true, resizable: true, showOverflowTooltip: true },
            { label: this.$t('resContrManage.ff'), prop: 'methodType', width: 100, sortable: true, resizable: true, dataCode: 'HTTP_METHOD_TYPE' },
            { label: this.$t('resContrManage.zjbgyh'), prop: 'lastChgName', width: 120, sortable: true, resizable: true },
            { label: this.$t('resContrManage.zjbgsj'), prop: 'lastChgDt', width: 120, sortable: true, resizable: true }
          ],
          pointFields: [{
            columnCount: 2,
            fields: [
              {
                field: 'contrName',
                label: this.$t('resContrManage.kzczmc'),
                rules: [
                  { required: true, message: this.$t('resContrManage.btx'), trigger: 'blur' },
                  { max: 90, message: this.$t('resContrManage.srzgc'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('resContrManage.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              {
                field: 'contrCode',
                label: this.$t('resContrManage.kzczdm'),
                rules: [
                  { required: true, message: this.$t('resContrManage.btx'), trigger: 'blur' },
                  { max: 45, message: this.$t('resContrManage.srzgc'), trigger: 'blur' },
                  { validator: yufp.validator.speChar, message: this.$t('resContrManage.srxxbhtszf'), trigger: 'blur' }
                ]
              },
              { field: 'lastChgName', label: this.$t('resContrManage.zjgxr'), hidden: true },
              { field: 'lastChgDt', label: this.$t('resContrManage.zjgxsj'), hidden: true }
            ]
          }, {
            columnCount: 2,
            fields: [
              {
                field: 'contrUrl',
                label: this.$t('resContrManage.kzcz'),
                rules: [
                  { max: 50, message: this.$t('resContrManage.srzgc'), trigger: 'blur' }
                ]
              },
              { field: 'methodType', label: this.$t('resContrManage.ff'), type: 'select', dataCode: 'HTTP_METHOD_TYPE' }
            ]
          }, {
            columnCount: 1,
            fields: [
              { field: 'contrRemark', label: this.$t('resContrManage.bz'), type: 'textarea', rows: 3, rules: [{ max: 450, message: this.$t('resContrManage.srzgc'), trigger: 'blur' }] }
            ]
          }],
          pointFormButtons: [
            {
              label: this.$t('resContrManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                me.saveCreateFn();
              }
            },
            {
              label: this.$t('resContrManage.bc'),
              type: 'primary',
              hidden: false,
              op: 'submit',
              click: function (model) {
                me.saveEditFn();
              }
            },
            {
              label: this.$t('resContrManage.qx'),
              hidden: false,
              click: function (model) {
                me.dialogVisible = false;
              }
            }
          ]
        };
      },
      methods: {
        // 左侧树节点点击事件,点击业务功能节点查询关联控制点
        nodeClickFn: function (nodeData, node, self) {
          var _this = this;
          _this.isChildNode = false;
          // update 添加F类型为子节点
          if (nodeData && nodeData.nodeType == 'F') {
            _this.isChildNode = true;
          }
          _this.funcId = nodeData.id;
          var _param = {};
          yufp.extend(_param, _this.pointFormdata);
          _param.funcId = _this.funcId ? _this.funcId : null;

          var param = {
            condition: _param
          };
          _this.$refs.pointTable.remoteData(param);
        },
        // 操作状态选择
        switchStatus: function (viewType, editable) {
          this.viewType = viewType;
          this.dialogVisible = true;
          this.formDisabled = !editable;
          this.pointFormButtons[2].hidden = !editable;
          if (viewType == 'ADD') {
            this.pointFormButtons[0].hidden = !editable;
            this.pointFormButtons[1].hidden = editable;
          } else if (viewType == 'EDIT') {
            this.pointFormButtons[0].hidden = editable;
            this.pointFormButtons[1].hidden = !editable;
          } else if (viewType == 'DETAIL') {
            this.pointFormButtons[0].hidden = !editable;
            this.pointFormButtons[1].hidden = !editable;
          }
        },
        // 不同状态最近更新人与时间字段显示控制
        hiddenSwitch: function (viewType, isHidden) {
          this.viewType = viewType;
          var fields = this.pointFields[0].fields;
          fields[2].hidden = !isHidden;
          fields[3].hidden = !isHidden;
        },
        // 控制点新增
        pointAddFn: function () {
          if (this.isChildNode == false) {
            this.$message({ message: this.$t('resContrManage.qxxzywgnjd'), type: 'warning' });
            return;
          }
          this.switchStatus('ADD', true);
          this.$refs.pointTable.clearSelection();
          this.hiddenSwitch('ADD', false);
          this.$nextTick(function () {
            this.$refs.pointForm.resetFn();
          });
        },
        saveCreateFn: function () {
          var _this = this;
          delete _this.$refs.pointForm.formModel.contrId;
          _this.$refs.pointForm.validate(function (valid) {
            if (valid) {
              _this.$refs.pointForm.formModel.lastChgUsr = yufp.session.userId;
              _this.$refs.pointForm.formModel.funcId = _this.funcId;
              yufp.service.request({
                method: 'GET',
                url: backend.appOcaService + '/api/adminsmrescontr/ifcoderepeat',
                data: {
                  funcId: _this.$refs.pointForm.formModel.funcId ? _this.$refs.pointForm.formModel.funcId : null,
                  contrCode: _this.$refs.pointForm.formModel.contrCode ? _this.$refs.pointForm.formModel.contrCode : null
                },
                callback: function (code, message, response) {
                  if (response.data.length > 0) {
                    _this.$message({
                      message: _this.$t('resContrManage.cywgnybhgkzczdm'),
                      type: 'warning'
                    });
                  } else {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmrescontr/createcontr',
                      data: _this.$refs.pointForm.formModel,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          _this.dialogVisible = false;
                          _this.$message({ message: _this.$t('resContrManage.sjbccg') });
                          var param = {
                            condition: JSON.stringify({
                              funcId: _this.funcId ? _this.funcId : null
                            })
                          };
                          _this.$refs.pointTable.remoteData(param);
                        }
                      }
                    });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('resContrManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 控制点修改
        modifyFn: function () {
          if (this.$refs.pointTable.selections.length < 1) {
            this.$message({ message: this.$t('resContrManage.qxxzytjl'), type: 'warning' });
            return;
          }
          if (this.$refs.pointTable.selections.length > 1) {
            this.$message({ message: this.$t('resContrManage.qxzytjl'), type: 'warning' });
            return;
          }
          this.switchStatus('EDIT', true);
          this.hiddenSwitch('EDIT', false);
          this.$nextTick(function () {
            this.$refs.pointForm.resetFn();
            yufp.extend(this.$refs.pointForm.formModel, this.$refs.pointTable.selections[0]);
          });
        },
        saveEditFn: function () {
          var _this = this;
          _this.$refs.pointForm.validate(function (valid) {
            if (valid) {
              _this.$refs.pointForm.formModel.lastChgUsr = yufp.session.userId;
              yufp.service.request({
                method: 'GET',
                url: backend.appOcaService + '/api/adminsmrescontr/ifcoderepeat',
                data: {
                  funcId: _this.$refs.pointForm.formModel.funcId ? _this.$refs.pointForm.formModel.funcId : null,
                  contrCode: _this.$refs.pointForm.formModel.contrCode ? _this.$refs.pointForm.formModel.contrCode : null,
                  contrId: _this.$refs.pointForm.formModel.contrId ? _this.$refs.pointForm.formModel.contrId : null
                },
                callback: function (code, message, response) {
                  if (response.data.length > 0) {
                    _this.$message({
                      message: _this.$t('resContrManage.cywgnybhgkzczdm'),
                      type: 'warning'
                    });
                  } else {
                    yufp.service.request({
                      method: 'POST',
                      url: backend.appOcaService + '/api/adminsmrescontr/editcontr',
                      data: _this.$refs.pointForm.formModel,
                      callback: function (code, message, response) {
                        if (code == '0' && response.code == 0) {
                          _this.dialogVisible = false;
                          _this.$message({ message: _this.$t('resContrManage.sjbccg') });
                          var param = {
                            condition: JSON.stringify({
                              funcId: _this.funcId ? _this.funcId : null
                            })
                          };
                          _this.$refs.pointTable.remoteData(param);
                        }
                      }
                    });
                  }
                }
              });
            } else {
              _this.$message({ message: _this.$t('resContrManage.qjcsrxsfhf'), type: 'warning' });
              return false;
            }
          });
        },
        // 点击控制操作名称列查看控制点详情
        rowClick: function (scope) {
          this.switchStatus('DETAIL', false);
          this.hiddenSwitch('DETAIL', true);
          this.$nextTick(function () {
            this.$refs.pointForm.resetFn();
            yufp.extend(true, this.$refs.pointForm.formModel, scope.row);
          });
        },
        // 左侧树节点类型图标
        renderContent: function () {
          var createElement = arguments[0];
          var type = arguments[1].data.nodeType;
          return createElement('span', [
            createElement('span', { attrs: { class: 'yu-treeMenuType' + type } }, type),
            createElement('span', arguments[1].node.label)
          ]
          );
        },
        // 控制点删除,删除前先判断是否有关联数据权限信息,如有不能进行删除
        pointDeleteFn: function () {
          if (this.$refs.pointTable.selections.length < 1) {
            this.$message({ message: this.$t('resContrManage.qxxzytjl'), type: 'warning' });
            return;
          }
          var _this = this;
          if (_this.$refs.pointTable.selections) {
            var ids = '';
            for (var i = 0; i < _this.$refs.pointTable.selections.length; i++) {
              ids = ids + ',' + _this.$refs.pointTable.selections[i].contrId;
            }
            _this.$confirm(_this.$t('resContrManage.sckzdjscqsjqxjsqsjqdsc'), _this.$t('resContrManage.ts'), {
              confirmButtonText: _this.$t('resContrManage.qd'),
              cancelButtonText: _this.$t('resContrManage.qx'),
              type: 'warning'
            }).then(function () {
              yufp.service.request({
                method: 'POST',
                data: {
                  id: ids
                },
                url: backend.appOcaService + '/api/adminsmrescontr/deletecontr/' + ids,
                callback: function (code, message, response) {
                  if (code == '0' && response.code == 0) {
                    _this.$message({ message: _this.$t('resContrManage.sjsccg') });
                    var param = {
                      condition: JSON.stringify({
                        funcId: _this.funcId ? _this.funcId : null
                      })
                    };
                    _this.$refs.pointTable.remoteData(param);
                  }
                }
              });
            });
          }
        }
      }
    });
  };
});
